#!/bin/bash
#(c) Copyright 2013 Barry Kauler, bkhome.org - GPL3 (/usr/share/doc/legal)
#
# 50-udev-puppy-basic.rules
# 
# env. variables: ACTION=add, DEVNAME=/dev/sda1,
#                 ID_PART_ENTRY_SIZE=15603712, ID_FS_TYPE=ntfs, ID_FS_LABEL=aaa etc..
#

#FCDEBUG=1
if [ "$FCDEBUG" = "1" ] ; then
	exec &>>/tmp/${0##*/}.log ; set -x ; env #debug
fi

ACTION=$1
DEV_NAME=$2
DRV_NAME=$2

case $DEV_NAME in
	sd*|hd*|mmc*|nvme*|sr*) ok=1 ;; #allowed devices
	*) exit 1 ;;
esac

. /etc/eventmanager #has RAMSAVEINTERVAL, HOTPLUGNOISY, BACKENDON, POWERTIMEOUT
if [ "$HOTPLUGON" != "true" -o "$BACKENDON" != "true" -o "$ICONDESK" != "true" ] ; then
	exit 1
fi

#==========================================================

if [ -f /tmp/pup_event_frontend_display ] ; then
	. /tmp/pup_event_frontend_display
fi
if [ -z "$DISPLAY" -o -z "$PATH" ] ; then
	echo "${0}: ERROR: empty "'$DISPLAY or $PATH'
	exit 1
fi
export DISPLAY PATH

. /etc/rc.d/functions_x #fx_functions
if fx_drv_is_optical ${DEV_NAME} ; then
	FC_OPTICAL_DRV=1
	[ "$ACTION" = "add" -a ! -e /sys/block/${DEV_NAME} ] && exit 1
else
	case $ACTION in add|change)
		[ ! -e /sys/block/${DEV_NAME} ] && exit 1 ;;
	esac
fi

if [ "$FCDEBUG" != "1" ] ; then
	# somehow this prevents the script from being in
	# a "zombie" state while pmount is running
	# and HOTPLUGNOISY is enabled...
	exec &>/dev/null
fi

#==========================================================

. /usr/local/pup_event/frontend_funcs
export FRONTEND_CHANGE=1

if [ "$ACTION" = "change" ] ; then
	UEVENT_CHANGE=1
	if [ "$FC_OPTICAL_DRV" = "1" ] ; then
		# frontend_timeout handles the situation if udev is misbehaving 
		# and not properly reporting hotplug events for optical drives
		if [ "$ID_CDROM_MEDIA_STATE" = "complete" ] ; then
			ACTION='add'
			# testing a bad optical drive there's a timing issue
			# and a race condition with frontend_timeout
			PS="`busybox ps`"
			echo "$PS" | grep "zzFRONTEND_CHANGE ${DRV_NAME}" && exit
		elif [ "$ID_CDROM_MEDIA" != "1" ] ; then
			ACTION='remove'
		else
			exit
		fi
	else
		case ${DEV_NAME} in sd*|hd*|mmc*|nvme*)
			#we want to completely refresh the desktop drive icons...
			remove_pinboard_func #needs DRV_NAME
			rm -rf /root/.pup_event/drive_${DEV_NAME}* 2>/dev/null
			ACTION='add' ;;
		esac
	fi
fi

#==========================================================

#create or remove desktop drive icons...
case $ACTION in
	add)

		[ -e /root/.pup_event/drive_${DEV_NAME} ] && exit #desktop icon (and partitions) already exist.

		PROBEDISK="$(probedisk -plus-size $DRV_NAME)"
		IFS="|" read -r DEV DRV_CATEGORY DRV_DESCRIPTION DRV_SIZE zz <<< "$PROBEDISK"

		if [ "$ICONPARTITIONS" = "true" ] ; then
			PROBEPART="`probepart -k -extra-info zzFRONTEND_CHANGE "${DRV_NAME}.*" | grep -E 'audiocd|btrfs|ext2|ext3|ext4|f2fs|hfs|iso9660|jfs|minix|msdos|ntfs|reiser|udf|vfat|exfat|xfs|crypto_LUKS'`" #130610 screen out unwante# cat /sys/block/sda
			[ -z "$PROBEPART" ] && exit 1
		fi

		#'startup' param just prevents func from running 'probepart'...
		create_icon_func startup #uses DEV_NAME, DRV_CATEGORY, DRV_DESCRIPTION

		if [ "$FC_OPTICAL_DRV" != "1" ] ; then # not an optical drv
			if [ "$UEVENT_CHANGE" = "1" ] ; then
				# the real uevent was 'change'
				# that uevent is only allowed for optical drvs
				exit
			fi
		fi

		if [ "$HOTPLUGNOISY" = "true" ] ; then
			/root/.pup_event/drive_${DEV_NAME}/AppRun ${DRV_CATEGORY} & #handler script.
		fi
		;;

	remove)
		[ ! -e /root/.pup_event/drive_${DEV_NAME} ] && exit #desktop icon (and partitions) already removed.
		if [ "`pidof gtkdialog_pmount`" != "" ];then #if pmount running, refresh it.
			killall gtkdialog_pmount 2>/dev/null
			sleep 0.1
			pmount & #100613 fix from shinobar.
		fi
		remove_pinboard_func #needs DEV_NAME
		rm -rf /root/.pup_event/drive_${DEV_NAME}* 2>/dev/null
		;;
esac

### END ###
